package com.example.smartcitydemo.ui.smartbus.fragment

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.blankj.utilcode.util.ToastUtils
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.bean.SmartBusRequestBean
import com.example.smartcitydemo.databinding.FragmentSmartBusFourthBinding

import com.example.smartcitydemo.viewModel.person.PersonViewModel
import com.example.smartcitydemo.viewModel.smartBus.SmartBusViewModel

/**
 * @PACKAGE_NAME: com.example.smartcity_jscs.ui.smartbus.fragment
 * @NAME: Caleb.F
 * @DATE: 2021/8/26 10:53
 * @Email: 1919796283@qq.com
 */
class SmartBusFourthFragment :
    BaseFragment<FragmentSmartBusFourthBinding>(R.layout.fragment_smart_bus_fourth) {
    private val smartBusViewModel: SmartBusViewModel by activityViewModels()
    private val personViewModel: PersonViewModel by activityViewModels()
    override fun addObservers(myViewBinding: FragmentSmartBusFourthBinding) {
        smartBusViewModel.timeString.observe(viewLifecycleOwner) {
            myViewBinding.apply {
                tvSmartBusTime.text = it
            }
        }

        personViewModel.personInfo.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                myViewBinding.apply {
                    it.data?.apply {
                        tvUserName.text = nickName
                        tvPhoneNum.text = phonenumber
                    }
                }
            }
        }
        smartBusViewModel.startStationData.observe(viewLifecycleOwner) {
            myViewBinding.apply {
                tvSmartBusStart.text = it
            }
        }
        smartBusViewModel.endStationData.observe(viewLifecycleOwner) {
            myViewBinding.apply {
                tvSmartBusEnd.text = it
            }
        }
        smartBusViewModel.choiceItemData.observe(viewLifecycleOwner) {

        }
        smartBusViewModel.busOrderResult.observe(viewLifecycleOwner) {
            if(it is Resource.Success){
                ToastUtils.showLong("新建订单成功,订单号:${it.data!!.orderNum}")
                this@SmartBusFourthFragment.requireActivity().finish()
            }
            else{
                ToastUtils.showShort("....")
            }
        }

    }

    override fun initView(
        myViewBinding: FragmentSmartBusFourthBinding,
        savedInstanceState: Bundle?
    ) {
        myViewBinding.apply {
            smartBusFourthBottomBar.btnSmartBusNext.text = "提交订单"
            toolbarSmartBusFourth.tvContent.text = "订单提交"
            typeBarBusFourthFirst.typeBarText.text = "个人信息"
            typeBarFourthSecond.typeBarText.text = "站点信息"
            smartBusFourthBottomBar.btnSmartBusPrevious.setOnClickListener {
                this@SmartBusFourthFragment.findNavController().navigateUp()
            }
            smartBusFourthBottomBar.btnSmartBusNext.setOnClickListener {
                smartBusViewModel.apply {
                    val busOrder = SmartBusRequestBean(
                        endStationData.value!!, choiceItemData.value!!.parentData.name,
                        choiceItemData.value!!.parentData.price.toString(),
                        startStationData.value!!, 1
                    )
                    smartBusViewModel.postBusOrder(busOrder)
                    this@SmartBusFourthFragment.requireActivity().finish()
                }

            }
            smartBusFourthBottomBar.btnSmartBusPrevious.setOnClickListener {
                this@SmartBusFourthFragment.findNavController().navigateUp()
            }
        }
    }

    override fun initData() {

    }
}